package cn.iocoder.yudao.module.resource.service.interactionlog;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.config.IConfigApi;
import cn.iocoder.yudao.module.infra.utils.EngineExecutorUtils;
import cn.iocoder.yudao.module.resource.api.enums.EnumAipError;
import cn.iocoder.yudao.module.resource.controller.admin.interactionlog.vo.InteractionLogPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.interactionlog.vo.InteractionLogSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.interactionlog.InteractionLogDO;
import cn.iocoder.yudao.module.resource.dal.mysql.interactionlog.InteractionLogMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 交互日志 Service 实现类
 *
 * @author lizhongwen
 */
@Slf4j
@Service
@Validated
public class InteractionLogServiceImpl implements InteractionLogService {
    private final ExecutorService logExecutors = EngineExecutorUtils.newThreadPoolExecutor(2, "interaction-log");

    private final String CONFIG_KEY_LOG_USER_WHITELIST = "log.whitelist.user";

    @Resource
    private InteractionLogMapper interactionLogMapper;

    @Resource
    private IConfigApi configApi;

    @Override
    public void asynCreateInteractionLog(InteractionLogSaveReqVO createReqVO) {
        final String userWhitelist = configApi.getValueByKey(CONFIG_KEY_LOG_USER_WHITELIST);
        if (StrUtil.isNotEmpty(userWhitelist) && !"all".equals(userWhitelist)) {
            final List<String> whitelist = Arrays.asList(userWhitelist.split(","));
            if (!whitelist.contains(createReqVO.getUserId().toString())) {
                return;
            }
        }

        logExecutors.execute(() -> {
            // 插入
            InteractionLogDO interactionLog = BeanUtils.toBean(createReqVO, InteractionLogDO.class);
            interactionLogMapper.insert(interactionLog);
            // 返回
            interactionLog.getLogId();
        });
    }

    @Override
    public void updateInteractionLog(InteractionLogSaveReqVO updateReqVO) {
        // 校验存在
        validateInteractionLogExists(updateReqVO.getLogId());
        // 更新
        InteractionLogDO updateObj = BeanUtils.toBean(updateReqVO, InteractionLogDO.class);
        interactionLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteInteractionLog(String id) {
        // 校验存在
        validateInteractionLogExists(id);
        // 删除
        interactionLogMapper.deleteById(id);
    }

    private void validateInteractionLogExists(String id) {
        if (interactionLogMapper.selectById(id) == null) {
            throw exception(EnumAipError.INTERACTION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public InteractionLogDO getInteractionLog(String id) {
        return interactionLogMapper.selectById(id);
    }

    @Override
    public PageResult<InteractionLogDO> getInteractionLogPage(InteractionLogPageReqVO pageReqVO) {
        return interactionLogMapper.selectPage(pageReqVO);
    }

}